<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编码" prop="identifier">
        <el-input
          v-model="queryParams.identifier"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
   

      <el-form-item label="商品类型" prop="marketEnable">
        <el-select v-model="queryParams.goodsType" placeholder="商品类型" clearable>
          <el-option
            v-for="dict in dict.type.goods_sku_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库存状态" prop="haveQuantity">
        <el-select v-model="queryParams.paramMap.haveQuantity" placeholder="库存状态" clearable>
          <el-option label="有" :value="true"/>
          <el-option label="无" :value="false"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- :disabled="!synchronizeOrders" -->
        <el-button
          type="success"
          plain
          icon="el-icon-grape"
          size="mini"
          @click="openPresentView"
          v-hasPermi="['offline:goods:specialty:sku:add']"
        >新增商品</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- :disabled="!synchronizeOrders" -->
        <el-button
          type="success"
          plain
          icon="el-icon-grape"
          size="mini"
          @click="handleSyncQuantityQuantity(ids)"
          v-hasPermi="['offline:goods:specialty:sku:delete']"
        >删除商品</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        :label="columns[0].label"
        align="left"
        :prop="columns[0].prop"
        :width="columns[0].width"
        :show-overflow-tooltip="true"
        v-if="columns[0].visible"
      />
      <el-table-column
        :label="columns[1].label"
        align="left"
        :prop="columns[1].prop"
        :width="columns[1].width"
        :show-overflow-tooltip="true"
        v-if="columns[1].visible"
      />
      <el-table-column
        :label="columns[2].label"
        align="left"
        :prop="columns[2].prop"
        :width="columns[2].width"
        :show-overflow-tooltip="true"
        v-if="columns[2].visible"
      />
      <el-table-column
        :label="columns[3].label"
        align="left"
        :prop="columns[3].prop"
        :width="columns[3].width"
        :show-overflow-tooltip="true"
        v-if="columns[3].visible"
      />
      <el-table-column
        :label="columns[4].label"
        align="left"
        :prop="columns[4].prop"
        :width="columns[4].width"
        :show-overflow-tooltip="true"
        v-if="columns[4].visible"
      />
      <el-table-column
        :label="columns[5].label"
        align="left"
        :prop="columns[5].prop"
        :width="columns[5].width"
        :show-overflow-tooltip="true"
        v-if="columns[5].visible"
      />
      <el-table-column
        :label="columns[6].label"
        align="left"
        :prop="columns[6].prop"
        :width="columns[6].width"
        :show-overflow-tooltip="true"
        v-if="columns[6].visible"
      />
      <el-table-column
        :label="columns[7].label"
        align="left"
        :prop="columns[7].prop"
        :width="columns[7].width"
        :show-overflow-tooltip="true"
        v-if="columns[7].visible"
      />
      <el-table-column
        :label="columns[8].label"
        align="left"
        :prop="columns[8].prop"
        :width="columns[8].width"
        :show-overflow-tooltip="true"
        v-if="columns[8].visible"
      />
      <el-table-column
        :label="columns[9].label"
        align="left"
        :prop="columns[9].prop"
        :width="columns[9].width"
        :show-overflow-tooltip="true"
        v-if="columns[9].visible"
      />
      <el-table-column
        :label="columns[10].label"
        align="left"
        :prop="columns[10].prop"
        :width="columns[10].width"
        :show-overflow-tooltip="true"
        v-if="columns[10].visible"
      />
      <el-table-column
        :label="columns[11].label"
        align="left"
        :prop="columns[11].prop"
        :width="columns[11].width"
        :show-overflow-tooltip="true"
        v-if="columns[11].visible"
      />
      <el-table-column
        :label="columns[12].label"
        align="left"
        :prop="columns[12].prop"
        :width="columns[12].width"
        :show-overflow-tooltip="true"
        v-if="columns[12].visible"
      />
      <el-table-column
        :label="columns[13].label"
        align="left"
        :prop="columns[13].prop"
        :width="columns[13].width"
        :show-overflow-tooltip="true"
        v-if="columns[13].visible"
      />
      <el-table-column
        :label="columns[14].label"
        align="left"
        :prop="columns[14].prop"
        :width="columns[14].width"
        :show-overflow-tooltip="true"
        v-if="columns[14].visible"
      />
      <el-table-column
        :label="columns[15].label"
        align="left"
        :prop="columns[15].prop"
        :width="columns[15].width"
        :show-overflow-tooltip="true"
        v-if="columns[15].visible"
      />
      <el-table-column
        :label="columns[16].label"
        align="left"
        :prop="columns[16].prop"
        :width="columns[16].width"
        :show-overflow-tooltip="true"
        v-if="columns[16].visible"
      />
      <el-table-column
        :label="columns[17].label"
        align="left"
        :prop="columns[17].prop"
        :width="columns[17].width"
        :show-overflow-tooltip="true"
        v-if="columns[17].visible"
      />
      <el-table-column
        :label="columns[18].label"
        align="left"
        :prop="columns[18].prop"
        :width="columns[18].width"
        :show-overflow-tooltip="true"
        v-if="columns[18].visible"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_sku_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>
      <el-table-column
        :label="columns[19].label"
        align="left"
        :prop="columns[19].prop"
        :width="columns[19].width"
        :show-overflow-tooltip="true"
        v-if="columns[19].visible"
      />
      <el-table-column
        :label="columns[20].label"
        align="left"
        :prop="columns[20].prop"
        :width="columns[20].width"
        :show-overflow-tooltip="true"
        v-if="columns[20].visible"
      />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 送赠品对话框 -->
    <el-dialog
      v-dialogDragWidth
      v-dialogDragHeight
      v-dialogDrag
      title="添加商品"
      :visible.sync="openPresentFlag"
      @click = "handlePresentList"
      width="1300px"
      append-to-body
    >
      <div class="gifts">

        <div class="left">
          
                
           商品编码:
          <el-input v-model="presentSkuListForm.identifier" style="width: 120px" clearable></el-input>商品名称:
          <el-input v-model="presentSkuListForm.goodsName" style="width: 200px" clearable></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handlePresentList"
            size="mini"
            style="margin: 0 0 20px 20px"
          >搜索</el-button>

          <el-table :data="presentSkuList" border>
            <el-table-column label="商品编码" prop="identifier" width="120" align="center"></el-table-column>
            <el-table-column label="商品名称" prop="goodsName" width="260" align="center"></el-table-column>
            <el-table-column label="规 格" prop="simpleSpecs" width="130" align="center"></el-table-column>
            <el-table-column label="库 存" prop="quantity" width="80" align="center"></el-table-column>
            <el-table-column label="生产厂家" prop="manufacturer" width="278" align="center"></el-table-column>
            <el-table-column label="操作" align="center"  width="130" >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  v-show="scope.row.addFlag"
                  @click="submitPresent(scope.row)"
                >添加</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="parseInt(presentSkuListForm.total) > 0"
            :total="parseInt(presentSkuListForm.total)"
            :page.sync="presentSkuListForm.pageNum"
            :limit.sync="presentSkuListForm.pageSize"
            @pagination="handlePresentList"
          />
        </div>

      </div>
      <div slot="footer" class="dialog-footer" >
        <el-button type="primary"  @click="closePresentView">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpecialty,addSpecialty,deleteSpecialty} from "@/api/goods/specialty";
import {
  getOrderInfos,
  getOrderIssueInfos,
  updOrderStatus,
  updOrderPayed,
  updateInnerStatus,
  getPayList,
  getLogisticsList,
  updLogistics,
  updUnpay,
  updPay,
  cancelOrder,
  addPresents,
  updFreightPrice
} from "@/api/order/order.js";
import { addAfterSale } from "@/api/order/afterSale.js";
import { listGoodsSku } from "@/api/goods/goods";

export default {
  dicts: ["goods_market_enable", "goods_sku_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        identifier: undefined,
        openPresentFlag: false,
        marketEnable: undefined,
        goodsType: undefined,
        paramMap: {
          haveQuantity: undefined
        }
      },
      presentSkuListForm: {
        identifier: "",
        goodsName: "",
        total:0,
        pageNum:1,
        pageSize: 10,
        goodsType:"NORMAL"
      },
      giftSkuListForm: {
        identifier: "",
        goodsName: "",
        total:0,
        pageNum:1,
        pageSize: 10
      },
      presentSkuList: [],
      listGoodsSku: [],
      filterGiftsList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      },
      // 列信息
      columns: [
        { key: 0, label: `商品ID`, prop: "id", width: 180, visible: false },
        {
          key: 1,
          label: `商品名称`,
          prop: "goodsName",
          width: 200,
          visible: true
        },
        {
          key: 2,
          label: `经营范围`,
          prop: "classification",
          width: 100,
          visible: true
        },
        { key: 3, label: `最低售价`, prop: "price", width: 80, visible: true },
        { key: 4, label: `成本价`, prop: "cost", width: 80, visible: false },
        {
          key: 5,
          label: `最低含税售价`,
          prop: "taxPrice",
          width: 100,
          visible: false
        },
        {
          key: 6,
          label: `商品编码`,
          prop: "identifier",
          width: 100,
          visible: true
        },
        { key: 7, label: `库存`, prop: "quantity", width: 70, visible: true },
        {
          key: 8,
          label: `计量单位`,
          prop: "goodsUnit",
          width: 80,
          visible: true
        },
        {
          key: 9,
          label: `规格`,
          prop: "simpleSpecs",
          width: 100,
          visible: true
        },
        {
          key: 10,
          label: `上架状态`,
          prop: "marketEnable",
          width: 80,
          visible: false
        },
        { key: 11, label: `条码`, prop: "barCode", width: 100, visible: false },
        {
          key: 12,
          label: `效期`,
          prop: "expirationDate",
          width: 100,
          visible: true
        },
        {
          key: 13,
          label: `商品分类`,
          prop: "assort",
          width: 80,
          visible: true
        },
        {
          key: 14,
          label: `生产厂家`,
          prop: "manufacturer",
          width: 180,
          visible: true
        },
        { key: 15, label: `产地`, prop: "place", width: 100, visible: true },
        { key: 16, label: `中包装`, prop: "pack", width: 60, visible: true },
        { key: 17, label: `件装量`, prop: "crate", width: 60, visible: true },
        {
          key: 18,
          label: `商品类型`,
          prop: "goodsType",
          width: 80,
          visible: true
        },
        {
          key: 19,
          label: `助记码`,
          prop: "mnemonicCode",
          width: 120,
          visible: true
        },
        {
          key: 20,
          label: `创建时间`,
          prop: "createTime",
          width: 160,
          visible: true
        }
      ],
      openPresentFlag: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 删除商品
    handleSyncQuantityQuantity(ids) {
this.$modal
        .confirm("确认删除吗？")
        .then(()=>{
          return deleteSpecialty(this.ids)
        }).then(()=>{
            this.$modal.msgSuccess("商品删除成功");
            this.getList();
        })
        .finally(()=>{
          this.openPresentFlag = false;
          this.ids = []
        })

    },
    handlePresentList() {
      if (this.presentSkuListForm.identifier) {
        this.presentSkuListForm.identifier = this.presentSkuListForm.identifier.trim();
      }
      if (this.presentSkuListForm.goodsName) {
        this.presentSkuListForm.goodsName = this.presentSkuListForm.goodsName.trim();
      }

      listGoodsSku(0, this.presentSkuListForm).then(response => {
        response.rows.forEach(item => {
          item.addFlag = true;
          for (let gift of this.listGoodsSku) {
            if (gift.id === item.id) {
              item.addFlag = false;
              break;
            }
          }
        });
        this.presentSkuList = response.rows;
        this.presentSkuListForm.total = response.total;
      });
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listSpecialty(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        identifier: undefined,
        marketEnable: undefined,
        goodsType: undefined,
        paramMap: {
          haveQuantity: undefined
        }
      };

      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
 
    },
    //打开赠送赠品窗口
    openPresentView(row) {
      this.openPresentFlag = true;
      this.handlePresentList();
      
    },
    closePresentView(){
     this.$modal
        .confirm("确认添加商品吗？")
        .then(()=>{
          return addSpecialty(this.listGoodsSku)
        }).then(()=>{
          this.openPresentFlag = false;
            this.$modal.msgSuccess("商品添加成功");
this.openPresentFlag =  false
            this.getList();
        })
        .finally(()=>{
          this.openPresentFlag = false;
          this.listGoodsSku = [];
          this.presentSkuListForm={};
        })
    },
    handleAddPresent(row) {
      this.$set(row, "num", 1);
      this.listGoodsSku.push(row);
      row.addFlag = false;

      let new_set = new Set(this.listGoodsSku);
      this.listGoodsSku = [...new_set];  
    },
    delGiftsItem(e) {
      let new_set = new Set(this.listGoodsSku);
      new_set.delete(e);
      this.listGoodsSku = [...new_set];
      e.addFlag = true;
    },
    //取消添加赠品
    abandonPresent() {
      this.openPresentFlag = false;
      this.listGoodsSku = [];


    },
    //提交赠品
    submitPresent(row) {
      this.listGoodsSku.push(row.identifier);
      row.addFlag = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.orderDetails_container {
  h1 {
    display: flex;
    margin: 30px 20px;
  }
  .orderDetails-orderInfo {
    padding: 0 20px;
    padding-bottom: 10px;
    border-bottom: 1px solid rgba($color: #000000, $alpha: 0.1);
    span {
      margin: 10px 0;
      margin-right: 50px;
    }
  }
  .tableOrderList {
    padding: 0 20px;
    // overflow: hidden;
  }
  .issueOrderList {
    padding: 0 20px;
    // overflow: hidden;
  }
  .p {
    text-align: center;
    padding: 50px 0;
    span {
      margin-right: 50px;
    }
  }
  .dict-tag {
    div {
      display: inline;
    }
  }
  .order_status {
    padding-right: 100px;
    float: right;
    margin-bottom: 100px;
  }
}
::v-deep .el-dialog {
  top: 0;
  .tableOrderList {
    height: 500px;
    overflow-y: scroll;
  }
  .p {
    padding: 3px 0 0;
  }
}

.gifts {
  display: flex;
  max-height: 80vh;
  .left {
    width: 1000px;
    margin-left: 100px;
     text-align: center;
   .flex-container {  
  display: flex;  
   text-align: center;
  flex-direction: column; /* 改为垂直方向 */  
}  ;
    .item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 30px;
    }
    span:nth-child(1) {
      max-width: 70%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      cursor: pointer;
      color: tomato;
    }
  }

}
</style>
